﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
using System.Configuration;

namespace ITSupportPlatform.DAL
{
    public class DataFromSharePoint
    {
        public dynamic GetNoticeById(string id)
        {
            List<dynamic> infos = GetNoticeList();
            dynamic info = infos.Where(x => x.SPId == id).SingleOrDefault();
            return info;
        }

        public List<dynamic> GetNoticeList()
        {
            ShartPointService service = SharePointHelper.GetService();
            XmlNode node = service.GetListItems(ConfigurationManager.AppSettings["NoticesListName"], String.Empty, null, ViewFields_Notice, "0", QueryOptions_Notice, String.Empty);
            XmlDocument doc = new XmlDocument();
            XmlNamespaceManager nsp = new XmlNamespaceManager(doc.NameTable);
            nsp.AddNamespace("rs", "urn:schemas-microsoft-com:rowset");
            nsp.AddNamespace("z", "#RowsetSchema");
            XmlNodeList nodeList = node.SelectNodes("rs:data/z:row", nsp);

            List<dynamic> returnInfos = new List<dynamic>();

            //FounderOAResourceGroupEntities context = new FounderOAResourceGroupEntities();

            foreach (XmlNode item in nodeList)
            {
                string id = item.Attributes["ows_ID"].Value;
                string title = item.Attributes["ows_Title"].Value;
                string body = item.Attributes["ows_Body"] == null ? "" : item.Attributes["ows_Body"].Value;
                string createTime = item.Attributes["ows_Created"].Value;
                string users = item.Attributes["ows_relevance"] == null ? "" : item.Attributes["ows_relevance"].Value;

                returnInfos.Add(new
                {
                    Id = Guid.NewGuid(),
                    Title = title,
                    Body = body,
                    CreateTime = DateTime.Parse(createTime),
                    SPId = id
                });
            }
            returnInfos.OrderByDescending(x => x.CreateTime);

            return returnInfos;
        }

        public List<dynamic> GetKnowledgeList()
        {
            ShartPointService service = SharePointHelper.GetService();
            XmlNode node = service.GetListItems(ConfigurationManager.AppSettings["KnowledgeListName"], String.Empty, null, ViewFields_Knowledge, "0", QueryOptions_Knowledge, String.Empty);
            XmlDocument doc = new XmlDocument();
            XmlNamespaceManager nsp = new XmlNamespaceManager(doc.NameTable);
            nsp.AddNamespace("rs", "urn:schemas-microsoft-com:rowset");
            nsp.AddNamespace("z", "#RowsetSchema");
            XmlNodeList nodeList = node.SelectNodes("rs:data/z:row", nsp);

            List<dynamic> returnInfos = new List<dynamic>();

            foreach (XmlNode item in nodeList)
            {
                string id = item.Attributes["ows_ID"].Value;
                string title = item.Attributes["ows_BaseName"].Value;
                string createTime = item.Attributes["ows_Created"].Value;
                string url = string.Format("http://zyinfo.founder.com/dept/IT/_layouts/DocIdRedir.aspx?ID={0}", item.Attributes["ows__dlc_DocId"].Value);

                returnInfos.Add(new
                {
                    Id = new Guid(),
                    Title = title,
                    CreateTime = DateTime.Parse(createTime),
                    Url = url,
                    SPId = id
                });
            }
            returnInfos.OrderByDescending(x => x.CreateTime);

            return returnInfos;
        }

        static XmlNode QueryOptions_Notice
        {

            get
            {

                XmlDocument _camlDoc = new XmlDocument();

                XmlElement queryOptions = _camlDoc.CreateElement("QueryOptions");

                queryOptions.InnerXml = "<ExpandUserField>True</ExpandUserField>";

                return queryOptions;

            }

        }
        static XmlNode ViewFields_Notice
        {

            get
            {
                XmlDocument _camlDoc = new XmlDocument();
                XmlElement viewFields = _camlDoc.CreateElement("ViewFields");

                System.Text.StringBuilder viewFieldsSB = new System.Text.StringBuilder();

                #region View Fields

                viewFieldsSB.Append(" <FieldRef Name=\"ID\" />");

                viewFieldsSB.Append(" <FieldRef Name=\"Title\" />");

                viewFieldsSB.Append(" <FieldRef Name=\"Body\" />");

                viewFieldsSB.Append(" <FieldRef Name=\"Created\" />");

                viewFieldsSB.Append(" <FieldRef Name=\"relevance\" />");

                #endregion

                string sResult = viewFieldsSB.ToString();

                viewFields.InnerXml = viewFieldsSB.ToString();

                return viewFields;

            }
        }
        
        static XmlNode QueryOptions_Knowledge
        {

            get
            {

                XmlDocument _camlDoc = new XmlDocument();

                XmlElement queryOptions = _camlDoc.CreateElement("QueryOptions");

                queryOptions.InnerXml = "<ExpandUserField>True</ExpandUserField>";

                return queryOptions;

            }

        }
        static XmlNode ViewFields_Knowledge
        {

            get
            {
                XmlDocument _camlDoc = new XmlDocument();
                XmlElement viewFields = _camlDoc.CreateElement("ViewFields");

                System.Text.StringBuilder viewFieldsSB = new System.Text.StringBuilder();

                #region View Fields

                viewFieldsSB.Append(" <FieldRef Name=\"ID\" />");

                viewFieldsSB.Append(" <FieldRef Name=\"BaseName\" />");

                viewFieldsSB.Append(" <FieldRef Name=\"_dlc_DocId\" />");

                viewFieldsSB.Append(" <FieldRef Name=\"Created\" />");

                #endregion

                string sResult = viewFieldsSB.ToString();

                viewFields.InnerXml = viewFieldsSB.ToString();

                return viewFields;

            }
        }
    
    }
}